package com.kingzoon.seckill.base.redis;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ch.qos.logback.classic.Logger;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Repository("redisDateSource")
public class RedisDataSourceImpl implements RedisDataSource {

	private static final Logger log = (Logger) LoggerFactory.getLogger(RedisDataSourceImpl.class);
	
	@Autowired
	private ShardedJedisPool shardedJedisPool;

	public ShardedJedis getRedisClient() {
		try {
			ShardedJedis shardJedis = shardedJedisPool.getResource();
			return shardJedis;
		}
		catch (Exception e) {
			log.error("getRedisClent error", e);
		}
		return null;
	}

	public void returnResource(ShardedJedis shardedJedis) {
		 //shardedJedisPool.returnResource(shardedJedis);
		 shardedJedis = shardedJedisPool.getResource();
	}

	public void returnResource(ShardedJedis shardedJedis, boolean broken) {
		if (broken) {
            //shardedJedisPool.returnBrokenResource(shardedJedis);
			shardedJedis = shardedJedisPool.getResource();
        } else {
            //shardedJedisPool.returnResource(shardedJedis);
        	shardedJedis = shardedJedisPool.getResource();
        }
	}

}
